package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.RoleMapper;
import com.cskaoyan.catmall.model.vo.RoleOptionsVo;
import com.cskaoyan.catmall.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cskaoyan.catmall.dao.RoleDao;
import com.cskaoyan.catmall.model.bo.PermissionBo;
import com.cskaoyan.catmall.model.bo.RoleBo;
import com.cskaoyan.catmall.model.vo.PermissionInfoVo;
import com.cskaoyan.catmall.model.vo.RoleVo;
import com.cskaoyan.catmall.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 创建日期: 2022-01-09-10-03
 * author : WQW
 * description: 角色相关的增删改查
 */

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    /**
     * @description
     * @author WQW
     * @date 2022/1/10 10:24
     * @param
     * @return com.cskaoyan.catmall.model.vo.RoleOptionsVo
     */
    @Override
    public RoleOptionsVo getAllRole() {
        final Integer PAGE = 1;
        final Integer PAGES = 1;
        List<RoleOptionsVo.ListDTO> listDTOS = roleMapper.selectAllRole();
        return new RoleOptionsVo(listDTOS.size(), PAGES, listDTOS.size(), PAGE, listDTOS);
    }
/**
 * @Author: 逍遥子
 * @Date: 2022/01/08/19:32
 * @Description:
 */


    @Autowired
    RoleDao roleDao;

    /**
     * @description 查询角色
     * @author czx
     * @date 2022-01-09 17:43
     * @param
     * @param roleBo
     * @return com.cskaoyan.catmall.model.vo.RoleVo
     */
    @Override
    public RoleVo selectRole(RoleBo roleBo) {

        Integer page = roleBo.getPage(); // 第几页
        Integer limit = roleBo.getLimit(); // 一页显示几条数据

        PageHelper.startPage(page, limit);
        List<RoleVo.ListDTO> listDTOS = roleDao.selectRole(roleBo);
        PageInfo<Object> pageInfo = new PageInfo(listDTOS);

        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        return new RoleVo(total, pages, limit, page, listDTOS);

    }

    /**
     * @description 添加角色
     * @author czx
     * @date 2022-01-09 17:45
     * @param
     * @param roleBo
     * @return int
     */
    @Override
    public int insertRole(RoleBo roleBo) {
        return roleDao.insertRole(roleBo);
    }

    /**
     * @description 删除角色
     * @author czx
     * @date 2022-01-09 17:45
     * @param
     * @param roleBo
     * @return int
     */
    @Override
    public int deleteRole(RoleBo roleBo) {
        return roleDao.deleteRole(roleBo);
    }

    /**
     * @description 更新权限
     * @author czx
     * @date 2022-01-09 17:45
     * @param
     * @param permissionBo
     * @return int
     */
    @Override
    public int updatePermission(PermissionBo permissionBo) {
        // 先查找有那些权限, 放在一个集合中
        String[] old = roleDao.selectPermission(permissionBo.getRoleId());
        Set<String> oldSet= new HashSet<>(Arrays.asList(old));
        // 新的权限, 也放到一个集合
        Set<String> newSet= new HashSet<>(Arrays.asList(permissionBo.getPermissions()));

        // 先对新权限保存
        Object[] setToArray = newSet.toArray();
        HashSet<Object> setCopy = new HashSet<>(Arrays.asList(setToArray));

        // 对集合求差集
        newSet.removeAll(oldSet);
        // 新增权限
        Object[] objects = newSet.toArray();
        if(objects.length == 0){
            // 没有新增的权限
        }else{
            // 新增权限
            roleDao.updatePermission(objects, permissionBo.getRoleId());
        }
        // 剔除权限信息
        oldSet.removeAll(setCopy);
        return roleDao.deletePermission(oldSet.toArray(), permissionBo.getRoleId());
    }

    /**
     * @description 查看用户权限
     * @author czx
     * @date 2022-01-09 17:46
     * @param
     * @param id
     * @return com.cskaoyan.catmall.model.vo.PermissionInfoVo
     */
    @Override
    public PermissionInfoVo selectPermission(Integer id) {

        // 查看用户拥有那些权限
        String[] arr = roleDao.selectPermission(id);

        // 查看系统权限
        List<PermissionInfoVo.SystemPermissionsDTO> systemPermissionsDTOS = roleDao.selectSystemPermissions(0);

        // 封装返回对象
        PermissionInfoVo permissionInfoVo = new PermissionInfoVo();
        permissionInfoVo.setAssignedPermissions(arr);
        permissionInfoVo.setSystemPermissions(systemPermissionsDTOS);

        return permissionInfoVo;
    }

    /**
     * @description 更新角色信息
     * @author czx
     * @date 2022-01-09 17:47
     * @param
     * @param roleBo
     * @return int
     */
    @Override
    public int updateRoleInfo(RoleBo roleBo) {
        return roleDao.updateRoleInfo(roleBo);
    }

}
